package ch.dreamweb.readpractice.client.lesetraining.train.presenter;

import ch.dreamweb.readpractice.client.lesetraining.Constants;
import ch.dreamweb.readpractice.client.lesetraining.train.view.Unit4View;
import ch.dreamweb.readpractice.client.lesetraining.train.view.UnitView;
import ch.dreamweb.readpractice.client.managed.request.ApplicationRequestFactory;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HasWidgets;

public class Unit4Presenter extends UnitPresenter implements
		Unit4View.Unit4Presenter {

	final Unit4View view;
	private final String[] morphems10 = { "", "", "", "", "", "", "", "", "",
			"" };
	private final int[] buttonNumbers = new int[20];
	private int record = 0;
	private int random = 0;
	private int index = 0;

	Timer t = new Timer() {
		@Override
		public void run() {
			Log.info("Timer 4 läuft ab");
			if (index < level) {
				view.resetColor();
				view.changeColor(buttonNumbers[index]);
				view.setMorphem(morphems10[index]);
				index++;
				t.schedule(prestime);
			} else {
				t.cancel();
				view.resetColor();
				view.setOkEnabled(false);
				view.setButtonsEnabled(true);
				random = (int) (Math.random() * level);
				view.setMorphem(morphems10[random]);
			}
		}
	};

	public Unit4Presenter(EventBus eventBus,
			ApplicationRequestFactory requests, Unit4View view) {
		super(eventBus, requests);
		this.view = view;
		this.view.setPresenter(this);
		// bind(); // not needed here
	}

	@Override
	protected void init() {
		super.init();
		view.setButtonsEnabled(false);
		// Infos aus Datenbank auslesen
		level = editableSetting.getT4level();
		step = editableSetting.getT4step();
		stepsperlevel = editableSetting.getT4stepsperlevel();
		highestlevel = editableSetting.getT4highestlevel();
		higheststep = editableSetting.getT4higheststep();
		highestdaylevel = editableSetting.getT4highestdaylevel();
		highestdaystep = editableSetting.getT4highestdaystep();
		tottime = editableSetting.getT4tottime();
		prestime = editableSetting.getT4prestime();
	}

	@Override
	public void go(final HasWidgets container) {
		super.go(container);
		container.add(view.asWidget());
	}

	@Override
	public void onOkButtonClicked() {
	}

	@Override
	public void onStartButtonClicked() {
		morphems = info.getMorphems();
		int[] buttons = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		for (int i = 0; i < 10; i++) {
			morphems10[i] = morphems.get(i);
			do {
				random = (int) (Math.random() * 10);
				buttonNumbers[i] = buttons[random];
				buttons[random] = 10;
			} while (buttonNumbers[i] == 10);
		}
		view.setOkEnabled(true);
		view.setStartEnabled(false);
		view.setInfo("");
		index = 0;
		t.schedule(500);

	}

	@Override
	public void onAnswerButtonClicked(int answerButtonNumber) {
		view.setButtonsEnabled(false);
		if (answerButtonNumber == buttonNumbers[random]) {
			record = increaseLevel();
			comment = commentCorrectFinal(record);
			view.setInfo(comment, Constants.CORRECT);
		} else {
			decreaseLevel();
			Log.info("in Unit4: Level: " + level + "    Step: " + step);
			comment = commentIncorrect();
			view.setInfo(comment, Constants.INCORRECT);
		}
		view.setStartEnabled(true);
		newExercice();
	}

	@Override
	protected void saveStuff() {
		super.saveStuff();

		editableSetting.setT4level(level);
		editableSetting.setT4step(step);
		editableSetting.setT4highestlevel(highestlevel);
		editableSetting.setT4higheststep(higheststep);
		editableSetting.setT4highestdaylevel(highestdaylevel);
		editableSetting.setT4highestdaystep(highestdaystep);
		editableSetting.setT4done(true);

		writeStatistic(4);
	}

	@Override
	protected void newExercice() {
		super.newExercice();
		if (info.isTimeUp(tottime)) {
			Log.info("Time's up... Ending Unit 4");
			saveStuff();
			info.setUnitActive(0);
			view.setMenuVisible(true);
			view.setFocusMenu();
			view.setStartEnabled(false);
		} else {
			view.setInfoLevel("Level: " + level + " / " + step);
			correctMorphems.clear();
			view.setMorphem("");
			view.setFocusStart();
		}
	}

	@Override
	protected UnitView getView() {
		return view;
	}

	@Override
	protected void showElephant(int type) {
		view.showElephant(type);
	}

}
